package com.justech.eat.service.daemons.impl;

import com.justech.eat.entity.Order;
import com.justech.eat.entity.OrderDefaultSet;
import com.justech.eat.entity.OrderDetail;
import com.justech.eat.model.UserOrderDefaultSetDto;
import com.justech.eat.service.OrderBatchService;
import com.justech.eat.service.component.OrderDefaultSupplier;
import com.justech.eat.service.daemons.AutoSubmitOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.constraints.Future;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: eat-service
 * @description:
 * @author: feifei.li
 * @create: 2020-06-11 14:54
 **/
@Component
public class DefaultAutoSubmitOrderPersever implements AutoSubmitOrderService {

    @Autowired
    OrderDefaultSupplier orderDefaultSupplier;
    @Autowired
    OrderBatchService orderBatchService;

    @Override
    public void startSubmitOrder(@Future(message = "日期必须在未来") String date) {
        if (date == null) {
            throw new RuntimeException("未指定自动提报日期，系统拒绝执行自动报餐请求！");
        }
        /*todo 查询行事历。如果当天为工作日，则继续。否则退出。*/
        /*获取所有用户的默认设置*/
        List<UserOrderDefaultSetDto> defaultSetDtos = orderDefaultSupplier.getList();
        /*排除已经自己订过的用户*/
        List<Order> beInserted = getBeInserted(defaultSetDtos, date);
        /*对未订过餐的用户设置订餐*/
        /*List<Order> inserted = orderBatchService.add(beInserted);*/
        /*存储订餐详情*/
        buildOrders(beInserted, defaultSetDtos);
        orderBatchService.add(beInserted);

    }

    /**
     * 建造报餐信息集合
     *
     * @param inserted
     * @param list
     */
    private void buildOrders(List<Order> inserted, List<UserOrderDefaultSetDto> list) {
        Map<String, OrderDefaultSet> userSetMap = list.stream().collect(Collectors.toMap(UserOrderDefaultSetDto::getJobNumber, UserOrderDefaultSetDto::getOrderDefaultSet));

        for (Order order : inserted) {
            List<OrderDetail> beInserted = new ArrayList<>(4);
            OrderDefaultSet defaultSet = userSetMap.get(order.getJobNumber());
            OrderDetail lunch = OrderDetail.build(order.getId(), 1, defaultSet.getLunch());
            OrderDetail dinner = OrderDetail.build(order.getId(), 2, defaultSet.getDinner());
            OrderDetail meal = OrderDetail.build(order.getId(), 3, defaultSet.getMeal());
            OrderDetail night = OrderDetail.build(order.getId(), 4, defaultSet.getNight());
            beInserted.add(lunch);
            beInserted.add(dinner);
            beInserted.add(meal);
            beInserted.add(night);
            order.setOrderDetails(beInserted);
        }

    }

    /**
     * 获取未设置过报餐信息的记录
     *
     * @param list
     * @param date
     * @return
     */
    public List<Order> getBeInserted(List<UserOrderDefaultSetDto> list, String date) {
        Map<String, Order> orderedMap = getOrderedMap(date);
        List<Order> beInserted = new ArrayList<>(list.size());
        for (UserOrderDefaultSetDto defaultSet : list) {
            //todo 查询企业行事历，判断该用户今天是否是工作日。
            Order order = orderedMap.get(defaultSet.getJobNumber());
            if (order == null) {
                beInserted.add(Order.build(defaultSet.getJobNumber(), date));
            }
        }
        return beInserted;
    }


    /**
     * 获取所有用户的订单map
     *
     * @param date
     * @return
     */
    private Map<String, Order> getOrderedMap(String date) {
        List<Order> orders = orderBatchService.getByDate(date);
        HashMap<String, Order> objectObjectHashMap = new HashMap<>(orders.size());
        for (Order order : orders) {
            objectObjectHashMap.put(order.getJobNumber(), order);
        }
        return objectObjectHashMap;
    }
}
